<template>
  <!-- 员工管理页 -->
  <div>
    <!-- 头部 -->
    <div class="header">
      <!-- 添加 -->
      <el-button type="primary" @click="addWaiter">添加</el-button>
      <!-- 输入姓名 -->
      <el-input
        placeholder="请输入用户名"
        style="width: 200px"
        v-model="userName"
        clearable
      >
      </el-input>
      <!-- 选择状态 -->
      <el-select v-model="userStatus" clearable placeholder="请选择状态">
        <el-option label="禁用" value="禁用"></el-option>
        <el-option label="正常" value="正常"></el-option>
      </el-select>
      <!-- 查询 -->
      <el-button type="primary" @click="queryWaiter">查询</el-button>
    </div>
    <!-- 内容 --> 
    <div class="tab_content">
      <el-table :data="tableData" stripe style="width: 100%">
        <el-table-column prop="id"  align="center" label="编号" width="180">
        </el-table-column>
        <el-table-column prop="username" align="center" label="用户名" width="180">
        </el-table-column>
        <el-table-column prop="realname" align="center" label="姓名"> </el-table-column>
        <el-table-column prop="telephone" align="center" label="手机号"> </el-table-column>
        <el-table-column prop="gender" align="center" label="性别"> 
            <template slot-scope="scope">
              <el-tag effect="plain" type="" v-if="scope.row.gender == 'male'">男</el-tag>
              <el-tag effect="plain" type="danger" v-else>女</el-tag>
            </template>
        </el-table-column>
        <el-table-column prop="status" align="center" label="状态"> 
        <template slot-scope="scope">
        <el-tag
          :type="scope.row.status === '正常' ? 'success' : 'danger'"
          effect="plain"
          size="small"
        >{{ scope.row.status }}</el-tag>
        </template>  
        </el-table-column>
        <el-table-column  align="center" label="操作">
          <template slot-scope="scope">
            <el-button  type="text" size="small" @click="edit(scope.row)">
              编辑
            </el-button>
            <el-button
              style="color: red"
              type="text"
              size="small"
              @click="deleteWaiter(scope.row.id)"
            >
              删除
            </el-button>
            <el-button
              style="color: blue"
              type="text"
              size="small"
              @click="routeDetails(scope.row.id)"
            >
              详情
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 尾部 -->
    <div class="paginetion">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[2, 4, 6, 8]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
    <!-- 弹出框 -->
    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="30%">
     <el-form :model="waiterForm" :rules="rules" ref="waiter_form" 
     label-width="100px" class="demo-ruleForm">
     <!--用户名-->
      <el-form-item label="用户名" prop="username">
          <el-input v-model="waiterForm.username"></el-input>
      </el-form-item> 
      <!--姓名-->
      <el-form-item label="姓名" prop="realname">
          <el-input v-model="waiterForm.realname"></el-input>
       </el-form-item>
       <!--手机号-->
      <el-form-item label="手机号" prop="telephone">
          <el-input v-model="waiterForm.telephone"></el-input>
      </el-form-item> 
      <!--密码-->
      <el-form-item label="密码" prop="password">
          <el-input v-model="waiterForm.password" type="password"></el-input>
      </el-form-item>
      <!-- 性别 -->
      <el-form-item label="性别" prop="gender">
          <el-radio v-model="waiterForm.gender" label="male" >男</el-radio>
          <el-radio v-model="waiterForm.gender" label="female" >女</el-radio>
      </el-form-item>
     <!-- 状态 -->
    <el-form-item v-if="flag == 1" label="状态" prop="status">
      <el-radio-group v-model="waiterForm.status">
        <el-radio label="正常"></el-radio>
        <el-radio label="禁用"></el-radio>
      </el-radio-group>
    </el-form-item>    
     </el-form>
      <span slot="footer" class="dialog-footer">
      <el-button  @click="dialogClose">取 消</el-button>
      <el-button v-if="flag==0" type="primary" @click="submitForm">确 定</el-button>
      <el-button v-else type="primary" @click="editWaiter(waiterForm)">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { get, post } from "@/utils/request";
export default {
  data() {
    return {
      userName: "",
      userStatus: "",
      tableData:[],
      currentPage:1,
      pageSize:8,
      total:0,
      queryData:{},
      dialogTitle:'添加员工',
      dialogVisible:false,
      //判断状态是否存在
      flag:0,
      //表单的内容
      waiterForm:{
        username:'',
        realname:'',
        telephone:'',
        password:'',
        gender:'',
        status:'',
        roleId:'',
      },
      role:{
        roleId:1
      },
      //表单判定规则
      rules: {
          username: [
            { required: true, message: '请输入用户名', trigger: 'blur' },
          ],
          name: [
            { required: true, message: '请输入姓名', trigger: 'blur' }
          ],
          telephone: [
            { required: true, message: '请输入手机号', trigger: 'blur' }
          ],
          password: [
            { required: true, message: '请输入密码', trigger: 'blur' },
          ],
          gender: [
            { required: true, message: '请选择性别', trigger: 'change' }
          ],
          status: [
            { required: true, message: '请选择状态', trigger: 'change' }
          ]
        }
    };
  },
  computed: {},
  methods: {
    //关闭表单
    dialogClose() {
      this.$refs['waiter_form'].resetFields();
      setTimeout(()=>{
         this.dialogVisible = false;
      },2000)
     },
    //提交表单
    submitForm() {
      this.$refs['waiter_form'].validate(async (valid) => {
          if (valid) {
          let data = {...this.waiterForm,...this.role}
           //发送表单验证的请求
           let res = await post('/baseUser/addUserWidthRole',data)
           //给用户一个用户提示
            if (res.status == 200) {
            this.$message({
              message: res.message,
              type: "success",
            });
            this.getAllWaiterData();
            } else {
            this.$message.error("错了哦，操作失误");
            }
           //刷新提示
           //调用关闭模态框的方法
           this.dialogVisible = false;
          } else {
            console.log('error submit!!');
            return false;
          }
      });
    },
    //添加员工
    addWaiter() {
      this.waiterForm = {}
      this.dialogVisible = true
      this.dialogTitle = '添加员工信息'
      this.flag = 0
    },
    edit(row){
      this.flag = 1
      this.dialogVisible = true
      this.dialogTitle = '编辑员工信息'
      this.waiterForm = {...row}
    },
    //编辑用户
    async editWaiter(row){
      let res =  await post('/baseUser/saveOrUpdate',row)
      this.dialogVisible = false;
      if(res.status == 200){
            this.$message({
              message: res.message,
              type: "success",
            });
      }
      this.getAllWaiterData()
    },
    handleSizeChange(val){
       this.pageSize = val;
       this.getAllWaiterData()
    },
    handleCurrentChange(val){
       this.currentPage = val;
       this.getAllWaiterData();
    },
    //查询员工所有数据
    async getAllWaiterData(){
       let data = {
         page:this.currentPage,
         pageSize:this.pageSize,
         roleId:1
       }
       let res = await get('/baseUser/pageQuery',{...data,...this.queryData})
       this.tableData = res.data.list
       this.total = res.data.total
    },
    //按条件查询
    queryWaiter(){
      let data = {
        username:this.userName,
        status:this.userStatus,
      }
      for (const key in data) {
        if (!data[key]) {
          delete data[key];
        }
      }
      this.queryData = data;
      this.currentPage = 1;
      this.getAllWaiterData()
    },
    //通过id删除
  deleteWaiter(val){
    this.$confirm('此操作将删除该员工, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async() => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
          await get('/baseUser/deleteById',{id:val})
          this.getAllWaiterData()
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });          
        });
    },
    //跳转到详情页面
    routeDetails(val) {
      this.$router.push(
        {
          path: 'Details',
          query:{
            id:val
          }
        }
      )
    }
  },
  created() {
    this.getAllWaiterData()
  },
  mounted() {},
};
</script>
<style scoped>
.paginetion {
  position: fixed;
  bottom: 40px;
  right: 10px;
}
</style>